import { _decorator, Component, Node, Prefab, instantiate, Input, EventTouch, Label } from 'cc';
import CCUtils from '../util/CCUtils';
import NativeUtils from '../util/NativeUtils';
import { CardItem } from './CardItem';
import { EventMgr } from './EventManager';
import { gameConst, GameEvent, typeArryAll } from './GameConst';
import { LayerItem } from './LayerItem';
import { LayerTabItem } from './LayerTabItem';
const { ccclass, property } = _decorator;

class Editor {
    
    private allCardContent:Node = null;

    private layerTabs:Node = null;

    private btnNewLayer:Node = null;

    private btnSave:Node = null;

    private btnGetConfig:Node = null;

    private layerContent:Node = null;

    private configNameLable:Label = null;


    public selectCard:CardItem = null;

    private currentEditorLayer:LayerItem = null;
    /**
     * init
     */
    public init(gameNode:Node) {

        this.allCardContent = CCUtils.findChild(gameNode,"ScrollView/view/allCardContent");
        this.layerTabs = CCUtils.findChild(gameNode,"layerTabs");
        this.layerContent = CCUtils.findChild(gameNode,"layerContent");
        this.btnNewLayer = CCUtils.findChild(gameNode,"btnNewLayer");
        this.btnNewLayer.on(Input.EventType.TOUCH_START, this.onClickBtnNewLayer, this);
        this.btnSave = CCUtils.findChild(gameNode,"btnSave");
        this.btnSave.on(Input.EventType.TOUCH_START, this.onClickBtnSave, this);

        this.configNameLable = CCUtils.getLabel(gameNode,"configNameLable");


        this.btnGetConfig = CCUtils.findChild(gameNode,"btnGetConfig");
        this.btnGetConfig.on(Input.EventType.TOUCH_START, this.onClickBtnGetConfig, this);
        
        

        EventMgr.addEventListener(GameEvent.selectLayer, this.selectLayer, this);
        EventMgr.addEventListener(GameEvent.delectLayer, this.delectLayer, this);



        this.configNameLable.string = "新配置";

        this.initAllCardItem();

        this.addLayer();
        
    }

    private initAllCardItem() {
        for (let index = 0; index < typeArryAll.length; index++) {
            let card:Node = instantiate(gameConst.globelPrefab.cardPrefab);
            let citem:CardItem = card.getComponent(CardItem);
            this.allCardContent.addChild(card);
            card.active = true;
            citem.init(null,typeArryAll[index],null);
            card.on(Input.EventType.TOUCH_START, this.onClickCard, this);
        }
        
    }

    private onClickBtnNewLayer(e:EventTouch) {
        this.addLayer();       
    }

    private onClickBtnSave(e:EventTouch) {
        
        NativeUtils.saveJson(this.getAllInfo());

        // NativeUtils.openFile();
        // this.addLayer();
        
    }


    private onClickBtnGetConfig(e:EventTouch) {
        NativeUtils.getConfig();
    }

    

    private addLayer() {
        let layer:Node = instantiate(gameConst.globelPrefab.layerPrefab);
        let item:LayerItem = layer.getComponent(LayerItem); 
        this.layerContent.addChild(layer);
        // this.layerContent.insertChild(layer,0);
        item.init();
        this.currentEditorLayer = item;

        
        let layerTab:Node = instantiate(gameConst.globelPrefab.layerTabPrefab);
        let itemTab:LayerTabItem = layerTab.getComponent(LayerTabItem); 
        this.layerTabs.addChild(layerTab);
        itemTab.init(item.info.layerId, item);
    }

    private onClickCard(e:EventTouch) {
        let item:CardItem = e.currentTarget.getComponent(CardItem);
        item.select = true;
        if (this.selectCard != null) {
            this.selectCard.select = false;
        }
        if (item != this.selectCard) {
            this.selectCard = item;
        }
        if (this.selectCard && this.currentEditorLayer) {
            this.currentEditorLayer.addNewCardToWaitContent(this.selectCard.type);
        }
    }

    private selectLayer(selectTab:LayerTabItem) {
        this.currentEditorLayer = selectTab.layer;
        // let alllayerTabs:Node = this.layerTabs.children;
        for (let index = 0; index < this.layerTabs.children.length; index++) {
            let tab:Node = this.layerTabs.children[index];
            if (tab != selectTab.node) {
                let itemTab:LayerTabItem = tab.getComponent(LayerTabItem);
                itemTab.setChoose(false);
            }
        }
    }


    private delectLayer(delectTab:LayerTabItem) {
        console.log("移除layer", delectTab);
        for (let index = 0; index < this.layerTabs.children.length; index++) {
            let tab:Node = this.layerTabs.children[index];
            if (tab == delectTab.node) { //找到了
                let itemTab:LayerTabItem = tab.getComponent(LayerTabItem);
                if (this.currentEditorLayer == itemTab.layer) {
                    this.currentEditorLayer = null;
                }
                itemTab.layer.node.removeFromParent();
            }
        }
        delectTab.node.removeFromParent();
    }



    /**
     * 获取所有卡牌的设置
     */
    private getAllInfo():any {
        let resule:any = {configId:Date.now(),layers:[]};
        for (let index = 0; index < this.layerTabs.children.length; index++) {
            let tab:Node = this.layerTabs.children[index];
            let itemTab:LayerTabItem = tab.getComponent(LayerTabItem);
            resule.layers.push(itemTab.layer.getAllInfo());
        }
        return resule;
    }


    
}

export let editor: Editor = new Editor();


